//
//  SYDDrivingLicenceViewController.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/7.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "SYDDrivingLicenceViewController.h"
#define LicenseInfoId @"SYDDriverLicenseDateCell"
@interface SYDDrivingLicenceViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    HcdDateTimePickerView * dateTimePickerView;
}
@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)SYDRegisterHeaderView *registerHeaderView;
@property(nonatomic,strong)SYDRegisterFooterView *registerFooterView;

@property(nonatomic,strong)NSString *runninglicenceFrontirstUrl;//行驶证前面
@property(nonatomic,strong)NSString *runninglicenceBackirstUrl;//行驶证后面
@property(nonatomic,strong)NSString *runningLicenceRegisterTime;//注册日期
@end

@implementation SYDDrivingLicenceViewController
#pragma mark - 导航栏
- (void)navigationBarHandle{
    
    WEAKSELF
    [weakSelf backButtonAction:^{
        
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationBar.title = @"注册";
    [self initlicenceUI];
    [self navigationBarHandle];
}
-(void)initlicenceUI{
    if (@available(iOS 11.0,*)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.runninglicenceFrontirstUrl = @"";
    self.runninglicenceBackirstUrl = @"";
    self.runningLicenceRegisterTime = @"";
    
    self.tableView.tableHeaderView = self.registerHeaderView;
    self.tableView.tableFooterView = self.registerFooterView;
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:LicenseInfoId bundle:nil] forCellReuseIdentifier:LicenseInfoId];
    WEAKSELF
    self.registerFooterView.nextStepBtn.eventTimeInterval  =3.0;
    [[self.registerFooterView.nextStepBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        [weakSelf UploadDriveInfoRegisterToServer];
    }];
    
    //获取头像
    [[self.registerFooterView.leftImage rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        [SYDPhotoPickerManager shared].maxCount = 1;
        [[SYDPhotoPickerManager shared]showActionSheetInView:self.view fromController:self allowEdit:YES completion:^(UIImage *image) {
            UIImage *newImage =  [UIImage imageWithImage:image scaledToSize:CGSizeMake(160, 100)];
            [weakSelf.registerFooterView.leftImage setImage:newImage forState:UIControlStateNormal];
            //上传图片
            [[SYDNetwork shareTool]uploadMultipartFilesWithParameters:nil mFile:@"mFile" uploadData:UIImageJPEGRepresentation(image, 0.01) success:^(NSURLSessionDataTask *task, id responseObject) {
                @try {
                    NSLog(@"responseObject:%@",responseObject);
                    if ([responseObject[@"statusCode"]integerValue] == 200) {
                        weakSelf.runninglicenceFrontirstUrl = responseObject[@"obj"];
                    }
                } @catch (NSException *exception) {
                    NSLog(@"exception:%@",exception.description);
                } @finally {
                }
            } failure:^(NSURLSessionDataTask *task, NSError *error) {
                NSLog(@"error:%@",error);
            }];
            [weakSelf.tableView reloadData];
        } cancelBlock:^{
            NSLog(@"cancel");
        }];
    }];
    
    [[self.registerFooterView.rightImage rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
       
        [SYDPhotoPickerManager shared].maxCount = 1;
        [[SYDPhotoPickerManager shared]showActionSheetInView:self.view fromController:self allowEdit:YES completion:^(UIImage *image) {
            UIImage *newImage =  [UIImage imageWithImage:image scaledToSize:CGSizeMake(160, 100)];
            [weakSelf.registerFooterView.rightImage setImage:newImage forState:UIControlStateNormal];
            //上传图片
            [[SYDNetwork shareTool]uploadMultipartFilesWithParameters:nil mFile:@"mFile" uploadData:UIImageJPEGRepresentation(image, 0.01) success:^(NSURLSessionDataTask *task, id responseObject) {
                @try {
                    NSLog(@"responseObject:%@",responseObject);
                    if ([responseObject[@"statusCode"]integerValue] == 200) {
                        weakSelf.runninglicenceBackirstUrl = responseObject[@"obj"];
                    }
                } @catch (NSException *exception) {
                    NSLog(@"exception:%@",exception.description);
                } @finally {
                }
            } failure:^(NSURLSessionDataTask *task, NSError *error) {
                NSLog(@"error:%@",error);
            }];
            [weakSelf.tableView reloadData];
        } cancelBlock:^{
            NSLog(@"cancel");
        }];
    }];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SYDDriverLicenseDateCell *cell = [tableView dequeueReusableCellWithIdentifier:LicenseInfoId];
    cell.leftTitle.text = @"注册日期";
    cell.rightDataLbl.text = [self.runningLicenceRegisterTime  isEqual: @""] ?@"请选择注册日期":self.runningLicenceRegisterTime;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return  cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SYDDriverLicenseDateCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    __block SYDDrivingLicenceViewController *weakSelf = self;
    
    dateTimePickerView = [[HcdDateTimePickerView alloc] initWithDatePickerMode:DatePickerDateMode defaultDateTime:[[NSDate alloc]initWithTimeIntervalSinceNow:100]];
    dateTimePickerView.topViewColor = WHITE_COLOR;
    dateTimePickerView.titleColor = TITLE_COLOR;
    dateTimePickerView.title = @"请选择注册日期";
    [dateTimePickerView setMinYear:2000];
    [dateTimePickerView setMaxYear:2022];
    dateTimePickerView.clickedOkBtn = ^(NSString * datetimeStr){
        NSLog(@"%@", datetimeStr);
        weakSelf.runningLicenceRegisterTime = datetimeStr;
        
        cell.rightDataLbl.text = datetimeStr;
    };
    
    if (dateTimePickerView) {
        [self.view addSubview:dateTimePickerView];
        [dateTimePickerView showHcdDateTimePicker];
    }
    [weakSelf.tableView reloadData];
}

#pragma mark---lazy
-(UITableView *)tableView{
    if (!_tableView) {
        if (iPhone8_X) {
            _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 88, SCREEN_WIDTH, SCREEN_HEIGHT - 88) style:UITableViewStylePlain];
        } else {
            _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
        }
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        
    }
    return  _tableView;
}
-(SYDRegisterHeaderView *)registerHeaderView{
    if (!_registerHeaderView) {
        _registerHeaderView = [[SYDRegisterHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 142)];
        _registerHeaderView.headerImage.image = [UIImage imageNamed:@"progress_bar3"];
        _registerHeaderView.titleMessageLbl.text = @"行驶证信息";
    }
    return _registerHeaderView;
}
-(SYDRegisterFooterView *)registerFooterView{
    if (!_registerFooterView) {
        _registerFooterView = [[SYDRegisterFooterView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 266)];
        _registerFooterView.titleLabel.text  = @"上传行驶证";
        [_registerFooterView.leftImage setImage:[UIImage imageNamed:@"vehicle_license_front"] forState:UIControlStateNormal];
        [_registerFooterView.rightImage setImage:[UIImage imageNamed:@"vehicle_license_back"] forState:UIControlStateNormal];

    }
    return _registerFooterView;
}
-(void)UploadDriveInfoRegisterToServer{
    WEAKSELF
    if ([self.runninglicenceFrontirstUrl isEqualToString:@""]){
        [SVProgressHUD displayInfoWithStatus:@"请上传行驶证正面"];
    }else if ([self.runninglicenceBackirstUrl isEqualToString:@""]){
        [SVProgressHUD displayInfoWithStatus:@"请上传行驶证反面"];
    }else if ([self.runningLicenceRegisterTime isEqualToString:@""]||self.runningLicenceRegisterTime.length == 0){
        [SVProgressHUD displayInfoWithStatus:@"请选择注册日期"];
    }else{
        
        [[SYDNetwork shareTool]userInfoRegisterWithRegisterStep:@"3" realName:@"" cardId:@"" iconUrl:@"" IDPhotoFrontUrl:@"" IDPhotoBackUrl:@"" busBrand:@"" busLicensePlate:@"" busOwner:@"" carPhotoUrl:@"" cityBelong:@"" cityCode:[YMSaveTool valueForKey:CityCode] firmBelong:@"" passengerCountPermitted:@"" drivingLicenceStartTime:@"" drivinglicenceBackUrl:@"" drivinglicenceFrontUrl:@"" runningLicenceRegisterTime:weakSelf.runningLicenceRegisterTime runningLicenceBackUrl:weakSelf.runninglicenceBackirstUrl runningLicenceFrontUrl:weakSelf.runninglicenceFrontirstUrl userId:self.registerID success:^(NSURLSessionDataTask *task, id responseObject) {
            @try {
                NSLog(@"%@",responseObject);
                if ([responseObject[@"statusCode"]integerValue] == 200) {
                    
                    SYDDriveViewController *driveVc = [[SYDDriveViewController alloc]init];
                    driveVc.registerID = weakSelf.registerID;
                    [self.navigationController pushViewController:driveVc animated:YES];
                }else{
                    [SVProgressHUD displayInfoWithStatus:responseObject[@"msg"]];
                }
            } @catch (NSException *exception) {
                NSLog(@"exception:%@",exception.description);
            } @finally {
                
            }
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"error:%@",error);
            [SVProgressHUD displayInfoWithStatus:@"注册失败,请检查网络..."];
        }];
        
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end







